<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>应急信息发布项目维护添加</title>
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <link href="../../layui/formSelects-v4.css" rel="stylesheet" />
	<script src="../../layui/formSelects-v4.js"></script>
    
    <script>
    		
			
        layui.use(['table', 'form', 'jquery', 'element','layer','laydate','upload'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            var upload = layui.upload;
            var layer=layui.layer;
            var user;
            var flagMgr;
            var regionName;
            var laydate = layui.laydate;
            var ss;
            var arrs = [];
    		var _tools = {
    			func1 : function() {
    				ss = sessionStorage.getItem('stationId');
    				arrs = ss.split(",");
    				console.log(arrs);
    				drawRelateTips();
    				var stationTips =getstationTips();
    				sessionStorage.setItem('alreadyRelate', stationTips);
    			}
    		}
    		
    		function getstationTips(){
    			var arr = new Array();
    				var i = 0;
    				$('.stationTips').each(function() {
    					arr[i] = $(this).html();
    					i = i + 1;
    				})
    				return arr.toString();
    		}
    		function drawRelateTips(){
    		console.log(arrs);
    		arrs.forEach(items=>{
    		
    					$('#remark').append(`<span style="margin:5px -10px 5px 5px;display:inline-block;"><span class="stationTips" style="color: #fff;background-color:#1E9FFF;padding:4px 6px;border-radius: 8px;">${items}</span><span class='delete' style="margin-right:10px;cursor:point;width:20px"><i class='layui-icon'>&#xe640;</i></span></span>`);
    				})
    				$('.delete').click(function(){
    					console.log(this.previousSibling.innerHTML);
    					arrs.splice(arrs.indexOf(this.previousSibling.innerHTML),1);
    					$(this).parent().remove();
    					sessionStorage.setItem('stationId', arrs.toString());
    					var stationTips =getstationTips();
    					sessionStorage.setItem('alreadyRelate', stationTips);
    					
    				})}
    		window.tools = _tools;
    		var mapManage = "/mapManage.html";
	        laydate.render({
	             elem: '#startTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        laydate.render({
	             elem: '#endTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        
	        function minDate(){
			    var now = new Date();
			    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
			}
			
			
            //根据城市ID查询扩展码
            function selectExtInfo(cityId) {
                var data;
                var url = '/util/findExdId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            console.log(json.data);
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接扩展码
            function appendExtId(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i] + '">' + data[i] + '</option>';
                    }
                    $('#extId').html(options);
                } else {
                    $('#extId').html('<option value="">该地市下没有扩展码</option>');
                }
                form.render();
            }
            //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                //appendCountyCheckBox(data2);
                var data3 = selectExtInfo(data.value);
                appendExtId(data3);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //
            function qryPathName() {
                var data;
                var url = '/smsArea/findPathName';
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                            if (data.length != null) {
                    		var options = '<option value="all">全部</option>';
                    			for (var i = 0; i < data.length; i++) {
                    				//console.info(data[i]);
                        			options += '<option value="' + data[i] + '">' + data[i] + '</option>';
                    			}
                    			$('#pathName').html(options);
                			} else {
                    			$('#pathName').html('暂无路线信息');
                			}
                form.render();
                            
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
            form.on('select(pathName)', function (data) {
                var subStationList=selectRegionName(data.value);
                appendSubStationNames(subStationList);
            });
            
            function selectRegionName(pathName) {
                var data;
                var url = '/smsArea/findRegionName?pathName=' + pathName;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
             function appendSubStationNames(data){
                if (data.length != null) {
                    var options = '<option value="all">全部</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i]+ '">' + data[i] + '</option>';
                    }
                    $('#subStationName').html(options);
                } else {
                    $('#subStationName').html('该路线下暂无点位');
                }
                form.render();
    		}
    
    			$("#regionName").autocomplete({
    			source : function(request, response) {
    				var pathName = $("#pathName").val();
    				var regionName = $("#regionName").val();
    				if("all"===pathName){
    					pathName="";
    				}
    				console.info(regionName);
    				var url = '/smsArea/findRegionNameLike';
    				var param = {
    					pathName : pathName,
    					regionName : regionName
    				};
    				$.ajax({
    					type : 'post',
    					async : false,
    					url : url,
    					data : param,
    					success : function(json) {
    						var data = json.data;
    						source = [];
    						for (var i = 0; i < data.length; i++) {
    							source[i] = data[i];
    						}
    						console.log(source);
    						response(source);
    					},
    					error : function() {
    						layer.msg('系统异常，请联系管理员处理!');
    					}
    				}
    				);
    			},
    			messages : { //message设置不出现匹配关键字的结果
    				noResults : '',
    				results : function() {}
    			}
    		});
    
    
    		//监听项目
    		form.on('select(countyId)', function (data) {
                //var data1 = selectMgrInfo(data.value);
                //appendMgr(data1);
            });
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }

			
			
			//判断项目名称是否重复
			function judgeMgrName(mgrName){
				var url = '/util/getMgrName';
			    $.ajax({
			            url: url,
			            type: 'post',
			            async: false,
			            data: "mgrName="+mgrName,
			            success: function (json) {
			                flagMgr = json.data;
			            }, error: function () {
			                layer.msg('系统异常，请联系管理员处理!');
			            }
			        }
			    );
    		}
    		$('#relate').on('click', function() {
    			
    			layer.open({
    				type : 2, //弹出iframe层
    				title : '关联基站信息',
    				area : [ '80%', '70%' ],
    				content : 'xuYiRegionRelate.html'
    			});
    		});
    
    		//插入提交
    		$('#submit').on('click', function() {
    			var regionName = $("#regionName").val();


    			
    			console.info(regionName);
    			var arr = new Array();
    			var i = 0;
    			$('.stationTips').each(function() {
    				arr[i] = $(this).html();
    				i = i + 1;
    			})


    			var stationTips = arr.toString();


    			console.info(stationTips);
    			var param = {
    				regionName : regionName,
    				stationTips : stationTips
    			};
    			//var index = layer.load(1, {shade: [0.1,'#fff'] });//0.1透明度的白色背景
    			var url = '/xuYiStation/stationRelateAdd';
    			var index;
    			$.ajax({
    				type : 'post',
    				data : param,
    				url : url,
    				async : false,
    				beforeSend : function() {
    					if (isNull(regionName)) {
    						layer.msg("点位名称不能为空");
    						return false;
    					}
    					
    					if (isNull(stationTips)) {
    						layer.msg("未关联基站！");
    						return false;
    					}
    					index = layer.load(1, {
    						shade : [ 0.1, '#fff' ]
    					}); //0.1透明度的白色背景
    				},
    				success : function(json) {
    					if (json.code == 0) {
    						layui.use('layer', function() {
    							layer.msg('添加成功,点击确认返回点位管理界面', {
    								time : 0, //不自动关闭
    								btn : [ '确定' ],
    								yes : function(index2) {
    									layer.close(index);
    									layer.close(index2);
    									location.href = "xuYiRegionManage.html";
    								}
    							});
    						});
    					} else {
    						layer.close(index);
    						layer.msg(json.msg);
    					}
    				},
    				complete : function() {},
    				error : function(data) {
    					layer.close(index);
    					layer.msg('系统异常，请联系管理员处理!');
    				}
    			});
    		});
    		
    		function findRelStationByRegion(regionName){
				var url = '/xuYiStation/findRelStationByRegion';
				var param = {
    				regionName : regionName
    			};
			    $.ajax({
			            url: url,
			            type: 'post',
			            async: false,
			            data: param,
			            success: function (json) {
			            	if(null!=json.data){
			            		arrs = json.data.split(",");
			            	}else{
			            		arrs =[];
			            	}
			                
			            }, error: function () {
			                layer.msg('系统异常，请联系管理员处理!');
			            }
			        }
			    );
    		}
    
    		//身份识别
    		$(document).ready(function() {
				sessionStorage.removeItem('alreadyRelate');
				
				regionName = decodeURI(decodeURIComponent(getUrlParam("regionName")));
				console.info(regionName);
				 $('#regionName').val(regionName);
				 findRelStationByRegion(regionName);
				 drawRelateTips();
				 sessionStorage.setItem('alreadyRelate', arrs.toString());
				 /* findRegionNameByPath(pathName);
				 console.info(arrs.toString());
				 drawRelateTips();
    			 sessionStorage.setItem('alreadyRelate', arrs.toString()); */
    		});

            $('#back').on('click', function (){
                window.location.href="xuYiRegionManage.html";
            });
    
    	});
    </script>
</head>
<body>
<div class="layui-html-limit">
    <!--container-->
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
                  <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#"> &nbsp;基站关联&nbsp;</a>
              </span>
            </div>
            <div class="content-box" style="height:100%;width:100%;">
                <div class="layui-form layui-form-pane" style="height:100%;width:110%;" >
                	<div id = "singleInsert">
                    <div class="layui-form-item" >
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">点位名称</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
			                    <input type="text" name="regionName" id="regionName" required lay-verify="required" disabled="disabled"
			                           placeholder="请输入路线名称"
			                           autocomplete="off" class="layui-input">
			                </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                    	<button class="layui-btn layui-btn-normal" id="relate">
                			<i class="layui-icon">&#xe608;</i> 关联基站
            			</button>
					</div>
                    <div class="layui-form-item">
                        <div class="layui-block">
                            <label class="layui-form-label" style="width: auto">&nbsp;已关联的基站</label>
                            <div class="layui-input-block">
                            	<!-- <textarea placeholder="暂无已关联的点位" class="layui-textarea" style="width: 30%"
                                      id="remark"></textarea> -->
                                 <div id="remark" style="border: 1px solid #333;margin-left: 10px;margin-top: 5px;width: 45%;min-height: 100px;max-height:300px;border-color: #e6e6e6;overflow:auto;}"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <!-- <button class="layui-btn" id="submit">立即提交</button> -->
                        <button class="layui-btn" id="submit">提交</button>
                        <button class="layui-btn" id="back">返回</button>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>